package org.puremvc.as3.multicore.patterns.mediator
{
	import com.swizipuremvc.core.ApplicationView;
	import com.swizipuremvc.core.common.MediatorType;
	import com.swizipuremvc.core.common.NoteType;
	import com.swizipuremvc.core.utils.ScopeUtil;
	
	import flash.utils.Dictionary;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.observer.Observer;

	public class AbstractMediator extends Mediator
	{
		//--------------------------------------------------------------------------------------------
		//--------------------------------------------------------------------------------------------
		private var _notificationHandlers:Dictionary;

		private var _scopeName:String;

		public var key:String;
		
		public var mediatorType:MediatorType;

		//--------------------------------------------------------------------------------------------
		public function AbstractMediator()
		{
			/*this.key = key;
			super(ScopeUtil.getName(scopeName, mediatorName), viewComponent);
			_notificationHandlers = new Dictionary();
			_scopeName = scopeName;
			this.initializeNotifier(key);*/
		}
		
		public override function initialize(key:String, mediatorName:String = null, viewComponent:Object = null, scopeName:String = null):void
		{
			this.key = key;
			super.initialize(key,mediatorName,viewComponent,scopeName);
			_notificationHandlers = new Dictionary();
			_scopeName = scopeName;
			this.initializeNotifier(key);
		}
		public function initializeNoteHandlers():void
		{
		
		}

		public function get scopeName():String
		{
			return _scopeName;
		}

		override public function listNotificationInterests():Array
		{
			var list:Array = [];
			for (var noteName:String in _notificationHandlers)
			{
				list.push(noteName);
			}
			return list;
		}

		override public function handleNotification(notification:INotification):void
		{
			var handler:Function = _notificationHandlers[notification.getName()];
			if (handler != null)
			{
				handler(notification);
			}
			else
			{
				handleNotificationByDefault(notification);
			}
		}

		//--------------------------------------------------------------------------------------------
		protected function handleNotificationByDefault(notification:INotification):void
		{
			throw new Error("Unexpected notification type: " + notification.getName());
		}

		protected function registerHandler(noteName:Object, handler:Function):void
		{
			ApplicationView.getInstance(key).registerObserver(noteName.toString(), new Observer(handler, this));
		}

		protected function removeHandler(noteName:Object):void
		{
			ApplicationView.getInstance(key).removeObserver(noteName.toString(), this);
		}

		protected function sendScopeNotification(noteType:NoteType, body:Object = null):void
		{
			sendNotification(ScopeUtil.getName(_scopeName, noteType.toString()), body);
		}

		protected function setNoteHandler(noteType:NoteType, noteHandler:Function):void
		{
			var scopName:* = ScopeUtil.getName(_scopeName,noteType.toString());
			_notificationHandlers[scopName] = noteHandler;
		}

		protected function setGlobalNoteHandler(noteType:NoteType, noteHandler:Function):void
		{
			_notificationHandlers[noteType.toString()] = noteHandler;
		}

		protected function get noteHandlers():Dictionary
		{
			return _notificationHandlers;
		}
	}
}
